package com.ruoyi.customermerchants.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.customer.domain.CrmCustomer;
import com.ruoyi.customer.enums.CrmCustomerStatusType;
import com.ruoyi.customer.mapper.CrmCustomerMapper;
import com.ruoyi.customermerchants.enums.MerchantsStatusType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.customermerchants.mapper.CrmCustomerMerchantsMapper;
import com.ruoyi.customermerchants.domain.CrmCustomerMerchants;
import com.ruoyi.customermerchants.service.ICrmCustomerMerchantsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户线索Service业务层处理
 * 
 * @author hct
 * @date 2022-02-16
 */
@Service
public class CrmCustomerMerchantsServiceImpl implements ICrmCustomerMerchantsService 
{
    @Autowired
    private CrmCustomerMerchantsMapper crmCustomerMerchantsMapper;

    @Autowired
    private CrmCustomerMapper crmCustomerMapper;

    /**
     * 查询客户线索
     * 
     * @param id 客户线索主键
     * @return 客户线索
     */
    @Override
    public CrmCustomerMerchants selectCrmCustomerMerchantsById(Long id)
    {
        return crmCustomerMerchantsMapper.selectCrmCustomerMerchantsById(id);
    }

    /**
     * 查询客户线索列表
     * 
     * @param crmCustomerMerchants 客户线索
     * @return 客户线索
     */
    @Override
    public List<CrmCustomerMerchants> selectCrmCustomerMerchantsList(CrmCustomerMerchants crmCustomerMerchants)
    {
        return crmCustomerMerchantsMapper.selectCrmCustomerMerchantsList(crmCustomerMerchants);
    }
    @Override
    public List<CrmCustomerMerchants> selectCrmCustomerMerchantsListN(CrmCustomerMerchants crmCustomerMerchants)
    {
        return crmCustomerMerchantsMapper.selectCrmCustomerMerchantsListByN(crmCustomerMerchants);
    }


    /**
     * 新增客户线索
     * 
     * @param crmCustomerMerchants 客户线索
     * @return 结果
     */
    @Override
    public int insertCrmCustomerMerchants(CrmCustomerMerchants crmCustomerMerchants)
    {
        return crmCustomerMerchantsMapper.insertCrmCustomerMerchants(crmCustomerMerchants);
    }

    /**
     * 修改客户线索
     * 
     * @param crmCustomerMerchants 客户线索
     * @return 结果
     */
    @Override
    public int updateCrmCustomerMerchants(CrmCustomerMerchants crmCustomerMerchants)
    {
        return crmCustomerMerchantsMapper.updateCrmCustomerMerchants(crmCustomerMerchants);
    }

    /**
     * 批量删除客户线索
     * 
     * @param ids 需要删除的客户线索主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerMerchantsByIds(Long[] ids)
    {
        return crmCustomerMerchantsMapper.deleteCrmCustomerMerchantsByIds(ids);
    }

    /**
     * 删除客户线索信息
     * 
     * @param id 客户线索主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerMerchantsById(Long id)
    {
        return crmCustomerMerchantsMapper.deleteCrmCustomerMerchantsById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer merchantsToCustomer(Long id) {
        Date now =new Date();
        CrmCustomerMerchants crmCustomerMerchants = crmCustomerMerchantsMapper.selectCrmCustomerMerchantsById(id);
        crmCustomerMerchants.setStatus(MerchantsStatusType.DELETE.getCode());
        int i = crmCustomerMerchantsMapper.updateCrmCustomerMerchants(crmCustomerMerchants);
        CrmCustomer crmCustomer = new CrmCustomer();
        crmCustomer.setCompanyAddress(crmCustomerMerchants.getCompanyAddress());
        crmCustomer.setCompanyType(crmCustomerMerchants.getCompanyType());
        crmCustomer.setCompanyName(crmCustomerMerchants.getCompanyName());
        crmCustomer.setCompanyDesc(crmCustomerMerchants.getRemark());
        crmCustomer.setEmail(crmCustomerMerchants.getEmail());
        crmCustomer.setIndustryname(crmCustomerMerchants.getIndustryname());
        crmCustomer.setIndustryId(crmCustomerMerchants.getIndustryId());
        crmCustomer.setMobile(crmCustomerMerchants.getMobile());
        crmCustomer.setName(crmCustomerMerchants.getName());
        crmCustomer.setPost(crmCustomerMerchants.getPost());
        crmCustomer.setScale(crmCustomerMerchants.getScale());
        crmCustomer.setAddress(crmCustomerMerchants.getAddress());
        crmCustomer.setStatus(CrmCustomerStatusType.DEAL.getCode());
        crmCustomer.setBusinessScope(crmCustomerMerchants.getBusinessScope());
        crmCustomer.setCompanystatus(crmCustomerMerchants.getCompanystatus());
        crmCustomer.setIncubatorTime(now);
        crmCustomer.setRegisterMoney(crmCustomerMerchants.getRegisterMoney());
        crmCustomer.setRegisterTime(crmCustomerMerchants.getRegisterTime());
        crmCustomer.setSocialCreditCode(crmCustomerMerchants.getSocialCreditCode());
        crmCustomer.setCompanyLogo(crmCustomerMerchants.getCompanyLogo());
        crmCustomer.setRegisterendTime(now);
        crmCustomer.setManagementType(crmCustomerMerchants.getManagementType());
        crmCustomer.setCompanyNature(crmCustomerMerchants.getCompanyNature());
        crmCustomer.setRegistrationAuthority(crmCustomerMerchants.getRegistrationAuthority());
        crmCustomer.setLegalPerson(crmCustomerMerchants.getLegalPerson());
        int i1 = crmCustomerMapper.insertCrmCustomer(crmCustomer);
        return i1;
    }
}
